package com.fenghuajueli.idiomppp.utils;

import android.content.Context;

import androidx.annotation.NonNull;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileUtil {

    public static void deleteFile(File file) {
        if (file.exists() && file.isFile()) {
            file.delete();
        } else if (file.exists() && file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File f : files) {
                    deleteFile(f);
                }
            }
            file.delete();
        }
    }

    public static long getFileSize(File file) {
        if (file.isFile()) {
            return file.length();
        }
        long size = 0;
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isDirectory()) {
                        size += getFileSize(files[i]);
                    } else {
                        size += files[i].length();
                    }
                }
            }
        }
        return size;
    }

    public static void copyFile(File file, String objPath) {
        FileOutputStream out = null;
        InputStream in = null;
        try {
            out = new FileOutputStream(objPath);
            in = new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            out.flush();
            out.close();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    public static void copyFile(Context context, String assetsPath, String objPath) {
        FileOutputStream out = null;
        InputStream in = null;
        try {
            out = new FileOutputStream(objPath);
            in = context.getAssets().open(assetsPath);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            out.flush();
            out.close();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    public static String getFileText(Context context, String assetsPath, String charset) {
        try {
            InputStream in = context.getAssets().open(assetsPath);
            int size = in.available();
            byte[] buffer = new byte[size];
            in.read(buffer);
            in.close();
            return new String(buffer, charset);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void copyFile2DirFromAssets(@NonNull Context context, @NonNull String assetsPath, @NonNull String destDirPath) {
        File destDir = new File(destDirPath);
        if (!destDir.exists() || !destDir.isDirectory()) {
            destDir.mkdirs();
        }
        String[] strings = assetsPath.split("/");
        String name = strings[strings.length - 1];
        try {
            strings = context.getAssets().list(assetsPath);
            if (strings == null || strings.length == 0) {// 文件
                String destPath = destDirPath + File.separator + name;
                copyFile2FileFromAssets(context, assetsPath, destPath);
            } else {// 文件夹
                File childDestDir = new File(destDir, name);
                for (String string : strings) {
                    copyFile2DirFromAssets(context, assetsPath + "/" + string, childDestDir.getAbsolutePath());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void copyFile2DirFromAssets(@NonNull Context context, @NonNull String assetsPath, @NonNull File destDir) {
        copyFile2DirFromAssets(context, assetsPath, destDir.getAbsolutePath());
    }

    public static String getFileTextFromAssets(@NonNull Context context, @NonNull String assetsPath, @NonNull String charset) {
        InputStream in = null;
        try {
            in = context.getAssets().open(assetsPath);
            int size = in.available();
            byte[] buffer = new byte[size];
            in.read(buffer);
            in.close();
            return new String(buffer, charset);
        } catch (IOException e) {
            e.printStackTrace();
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
        return null;
    }

    public static String getFileTextFromAssets(@NonNull Context context, @NonNull String assetsPath) {
        return getFileTextFromAssets(context, assetsPath, "utf-8");
    }

    public static void copyFile2FileFromAssets(@NonNull Context context, @NonNull String assetsPath, @NonNull String destPath) {
        FileOutputStream out = null;
        InputStream in = null;
        try {
            out = new FileOutputStream(destPath);
            in = context.getAssets().open(assetsPath);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            out.flush();
            out.close();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}
